﻿using System;
using System.Linq;
using System.Windows.Forms;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.CanBoQuanLyForm;
using ComputerVision.CapQuanLyHanhChinhForm;
using ComputerVision.Data;
using ComputerVision.HinhThucDauTuForm;
using ComputerVision.LoaiCoSoForm;
using ComputerVision.LoaiHinhHoatDongForm;
using ComputerVision.NganhForm;
using ComputerVision.QuanHuyenForm;
using ComputerVision.Services;
using ComputerVision.ThanhPhanKinhTeForm;
using PagedList;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmPhieuDieuTraCoSoList : frmSearch
    {
        #region Variable
        private IService<PhieuDieuTraCoSo> _phieuDieuTraCoSo;
        private readonly IService<LoaiCoSo> _loaiCoSo;
        private readonly IService<Nganh> _nganh;
        private readonly IService<CanBo> _canBo;
        private readonly IService<QuanHuyen> _quanHuyen;
        private readonly IService<CapQuanLyHanhChinh> _capQuanLyHanhChinh;
        private readonly IService<HinhThucDauTu> _hinhThucDauTu;
        private readonly IService<ThanhPhanKinhTe> _thanhPhanKinhTe;
        private readonly IService<LoaiHinhHoatDong> _loaiHinhHoatDong;
        private readonly IService<ThamDuyetPCCC> _thamDuyetPcCc;
        private readonly IService<HangMuc> _hangMuc;
        private readonly IService<PhuongTienCoSo> _phuongTienCoSo;
        private readonly IService<NguonNuoc> _nguonNuoc;
        private readonly IService<TuyenTruyenHuanLuyen> _tuyenTruyen;
        readonly IService<KiemTraAnToan> _kiemTra;
        private readonly IService<PhuongAnChuaChay> _phuongan;
        private readonly IService<XuLyViPham> _xuLyViPham;
        private readonly IService<TinhHinhChayNo> _tinhHinhChayNo;
        private readonly IService<ThayDoiCoSo> _thayDoiCoSo;
        int pageNumber = 1;
        #endregion

        #region Constructor
        public frmPhieuDieuTraCoSoList()
        {
            InitializeComponent();
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            _loaiCoSo = Program.Container.Resolve<IService<LoaiCoSo>>();
            _nganh = Program.Container.Resolve<IService<Nganh>>();
            _canBo = Program.Container.Resolve<IService<CanBo>>();
            _quanHuyen = Program.Container.Resolve<IService<QuanHuyen>>();
            _capQuanLyHanhChinh = Program.Container.Resolve<IService<CapQuanLyHanhChinh>>();
            _hinhThucDauTu = Program.Container.Resolve<IService<HinhThucDauTu>>();
            _thanhPhanKinhTe = Program.Container.Resolve<IService<ThanhPhanKinhTe>>();
            _loaiHinhHoatDong = Program.Container.Resolve<IService<LoaiHinhHoatDong>>();
            _thamDuyetPcCc = Program.Container.Resolve<IService<ThamDuyetPCCC>>();
            _hangMuc = Program.Container.Resolve<IService<HangMuc>>();
            _phuongTienCoSo = Program.Container.Resolve<IService<PhuongTienCoSo>>();
            _nguonNuoc = Program.Container.Resolve<IService<NguonNuoc>>();
            _tuyenTruyen = Program.Container.Resolve<IService<TuyenTruyenHuanLuyen>>();
            _kiemTra = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _phuongan = Program.Container.Resolve<IService<PhuongAnChuaChay>>();
            _xuLyViPham = Program.Container.Resolve<IService<XuLyViPham>>();
            _tinhHinhChayNo = Program.Container.Resolve<IService<TinhHinhChayNo>>();
            _thayDoiCoSo = Program.Container.Resolve<IService<ThayDoiCoSo>>();

            LoadQuanHuyen();
            LoadCanBo();
            LoadCapQuanLy();
            LoadHinhThucDauTu();
            LoadLoaiCoSo();
            LoadLoaiHinhHd();
            LoadNganh();
            LoadThanhPhanKte();
        }
        #endregion

        #region Add
        protected override void Add()
        {
            var frm = new frmPhieuDieuTraCoSoDetail();
            frm.MdiParent = MdiParent;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        #endregion

        #region Edit
        protected override void Edit()
        {
            EditData();
        }

        private void EditData()
        {
            if (dgvPhieuDieuTraCoSo.SelectedRows.Count > 0)
            {
                var selectedRow = dgvPhieuDieuTraCoSo.SelectedRows[0];
                var id = Guid.Parse(selectedRow.Cells[0].Value.ToString());
                var fEdit = new frmPhieuDieuTraCoSoDetail(id);
                fEdit.OnLoadData += LoadData;
                fEdit.MdiParent = MdiParent;
                fEdit.WindowState = FormWindowState.Maximized;
                fEdit.Show();
            }
            else
            {
                AlertList.Error("Lỗi trong quá trình sửa dữ liệu");
            }
        }
        #endregion

        #region SearchData
        protected override void SearchData()
        {
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            var phieuDieuTraList = _phieuDieuTraCoSo.ToList().ToList();
            Guid id;

            if (phieuDieuTraList.Any())
            {
                if (cbLoaiCoSo.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbLoaiCoSo.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.LoaiCoSoId.Equals(id)).ToList();
                }

                //if (cbQuanHuyen.SelectedValue != null)
                //{
                //    id = Guid.Parse(cbQuanHuyen.SelectedValue.ToString());

                //    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.quan.Equals(id)).ToList();
                //}

                if (cbNganhNghe.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbNganhNghe.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.NganhId.Equals(id)).ToList();
                }

                if (cbCanBoQuanLy.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbCanBoQuanLy.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.CanBoQuanLyId.Equals(id)).ToList();
                }

                if (cbCapQuanLyHanhChinh.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbCapQuanLyHanhChinh.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.CapQuanLyHanhChinhId.Equals(id)).ToList();
                }

                if (cbHinhThucDauTu.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbHinhThucDauTu.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.HinhThucDauTuId.Equals(id)).ToList();
                }

                if (cbThanhPhanKinhTe.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbThanhPhanKinhTe.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.ThanhPhanKinhTeId.Equals(id)).ToList();
                }

                if (cbLoaiHinhHD.SelectedValue.ToString() != Guid.Empty.ToString())
                {
                    id = Guid.Parse(cbLoaiHinhHD.SelectedValue.ToString());

                    phieuDieuTraList = phieuDieuTraList.ToList().Where(p => p.LoaiHinhHoatDongId.Equals(id)).ToList();
                }
            }

            phieuDieuTraCoSoBindingSource.DataSource = phieuDieuTraList;
        }
        #endregion

        #region Delete
        protected override void Delete()
        {
            var items = _phieuDieuTraCoSo.ToList().ToList().Where(p => p.Status.Equals(false));
            foreach (var item in items)
            {
                var thamDuyet = _thamDuyetPcCc.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var thamDuyetPccc in thamDuyet)
                {
                    _thamDuyetPcCc.Delete(thamDuyetPccc);
                }

                var hangMuc = _hangMuc.ToList().ToList().Where(p => p.PhieuDieuTraCoSo_Id.Equals(item.Id));
                foreach (var hm in hangMuc)
                {
                    _hangMuc.Delete(hm);
                }

                var phuongTien = _phuongTienCoSo.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var pt in phuongTien)
                {
                    _phuongTienCoSo.Delete(pt);
                }

                var nguonNuoc = _nguonNuoc.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (var nn in nguonNuoc)
                {
                    _nguonNuoc.Delete(nn);
                }

                var tuyenTruyen = _tuyenTruyen.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var tt in tuyenTruyen)
                {
                    _tuyenTruyen.Delete(tt);
                }

                var kiemTra = _kiemTra.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (var tt in kiemTra)
                {
                    _kiemTra.Delete(tt);
                }

                var phuongan = _phuongan.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var tt in phuongan)
                {
                    _phuongan.Delete(tt);
                }

                var xuLyViPham = _xuLyViPham.ToList().ToList().Where(p => p.PhieuDieuTraCoSoesId.Equals(item.Id));
                foreach (var tt in xuLyViPham)
                {
                    _xuLyViPham.Delete(tt);
                }

                var tinhHinhChayNo = _tinhHinhChayNo.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var tt in tinhHinhChayNo)
                {
                    _tinhHinhChayNo.Delete(tt);
                }

                var thayDoiCoSo = _thayDoiCoSo.ToList().ToList().Where(p => p.PhieuDieuTraCoSoId.Equals(item.Id));
                foreach (var tt in thayDoiCoSo)
                {
                    _thayDoiCoSo.Delete(tt);
                }

                _phieuDieuTraCoSo.Delete(item);
                LoadData();
            }

        }
        #endregion

        #region LoadData
        protected override void LoadData()
        {
            _phieuDieuTraCoSo = Program.Container.Resolve<IService<PhieuDieuTraCoSo>>();
            phieuDieuTraCoSoBindingSource.DataSource = _phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20);
        }
        #endregion
        

        #region Load Data to combobox

        public void LoadLoaiCoSo()
        {
            var loaiCoSo = _loaiCoSo.ToList().ToList().OrderBy(c => c.Ten).ToList();
            loaiCoSo.Insert(0, new LoaiCoSo());
            cbLoaiCoSo.DataSource = loaiCoSo;
            cbLoaiCoSo.DisplayMember = "Ten";
            cbLoaiCoSo.ValueMember = "Id";
        }

        public void LoadQuanHuyen()
        {
            var quanHuyen = _quanHuyen.ToList().ToList().OrderBy(n => n.Ten).ToList();
            quanHuyen.Insert(0, new QuanHuyen());
            cbQuanHuyen.DataSource = quanHuyen;
            cbQuanHuyen.DisplayMember = "Ten";
            cbQuanHuyen.ValueMember = "Id";
        }

        public void LoadNganh()
        {
            var nganh = _nganh.ToList().ToList().OrderBy(n => n.Ten).ToList();
            nganh.Insert(0, new Nganh());
            cbNganhNghe.DataSource = nganh;
            cbNganhNghe.DisplayMember = "Ten";
            cbNganhNghe.ValueMember = "Id";
        }

        public void LoadCanBo()
        {
            var canBo = _canBo.ToList().ToList().OrderBy(n => n.HoTen).ToList();
            canBo.Insert(0, new CanBo());
            cbCanBoQuanLy.DataSource = canBo;
            cbCanBoQuanLy.DisplayMember = "HoTen";
            cbCanBoQuanLy.ValueMember = "Id";
        }

        public void LoadCapQuanLy()
        {
            var capQuanLyHanhChinh = _capQuanLyHanhChinh.ToList().ToList().OrderBy(n => n.Ten).ToList();
            capQuanLyHanhChinh.Insert(0, new CapQuanLyHanhChinh());
            cbCapQuanLyHanhChinh.DataSource = capQuanLyHanhChinh;
            cbCapQuanLyHanhChinh.DisplayMember = "Ten";
            cbCapQuanLyHanhChinh.ValueMember = "Id";
        }

        public void LoadHinhThucDauTu()
        {
            var hinhThucDauTu = _hinhThucDauTu.ToList().ToList().OrderBy(n => n.Ten).ToList();
            hinhThucDauTu.Insert(0, new HinhThucDauTu());
            cbHinhThucDauTu.DataSource = hinhThucDauTu;
            cbHinhThucDauTu.DisplayMember = "Ten";
            cbHinhThucDauTu.ValueMember = "Id";
        }

        public void LoadThanhPhanKte()
        {
            var thanhPhanKinhTe = _thanhPhanKinhTe.ToList().ToList().OrderBy(n => n.Ten).ToList();
            thanhPhanKinhTe.Insert(0, new ThanhPhanKinhTe());
            cbThanhPhanKinhTe.DataSource = thanhPhanKinhTe;
            cbThanhPhanKinhTe.DisplayMember = "Ten";
            cbThanhPhanKinhTe.ValueMember = "Id";
        }

        public void LoadLoaiHinhHd()
        {
            var loaiHinhHoatDong = _loaiHinhHoatDong.ToList().ToList().OrderBy(n => n.Ten).ToList();
            loaiHinhHoatDong.Insert(0, new LoaiHinhHoatDong());
            cbLoaiHinhHD.DataSource = loaiHinhHoatDong;
            cbLoaiHinhHD.DisplayMember = "Ten";
            cbLoaiHinhHD.ValueMember = "Id";
        }

        #endregion

        #region Method
        private void btnLoaiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiCoSoDetail(this);
            frm.ShowDialog();
        }

        private void btQuanHuyen_Click(object sender, EventArgs e)
        {
            var frm = new frmQuanHuyenDetail(this);
            frm.ShowDialog();
        }

        private void btNganh_Click(object sender, EventArgs e)
        {
            var frm = new frmNganhDetail(this);
            frm.ShowDialog();
        }

        private void btCanBo_Click(object sender, EventArgs e)
        {
            var frm = new frmCanBoQuanLyDetail(this);
            frm.ShowDialog();
        }

        private void btCapQly_Click(object sender, EventArgs e)
        {
            var frm = new frmCapQuanLyHanhChinhDetail(this);
            frm.ShowDialog();
        }

        private void btThanhPhanKte_Click(object sender, EventArgs e)
        {
            var frm = new frmThanhPhanKinhTeDetail(this);
            frm.ShowDialog();
        }

        private void btHinhthucDauTu_Click(object sender, EventArgs e)
        {
            var frm = new frmHinhThucDauTuDetail(this);
            frm.ShowDialog();
        }

        private void btLoaiHinhHd_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiHinhHoatDongDetail(this);
            frm.ShowDialog();
        }
        #endregion

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (!_phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20).IsFirstPage)
            {
                pageNumber = 1;
                LoadData();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20).HasNextPage)
            {
                pageNumber++;
                LoadData();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (!_phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20).IsLastPage)
            {
                pageNumber = _phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20).PageCount;
                LoadData();
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (_phieuDieuTraCoSo.ToList().ToPagedList(pageNumber, 20).HasPreviousPage)
            {
                pageNumber--;
                LoadData();
            }
        }
    }
}
